//
//  FourthViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "FourthViewController.h"
#import "DebarkViewController.h"
#import "SDImageCache.h"
#import "CollectViewController.h"
#import "DateBaseSingleton.h"


@interface FourthViewController ()<UITableViewDataSource, UITableViewDelegate, FourthViewControllerDelegate>
@property(nonatomic, retain) UITableView *myTableView;

@end

@implementation FourthViewController
- (void)dealloc
{
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.title = @"我的";
    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *filePath = @"cell";
    UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:filePath];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的电影";

    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"清理缓存";
        
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

-(void)precentFouthViewController: (NSInteger) num{
    
    if (num == 1) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction1:)];
        
    }
}
- (void)rightAction: (UIBarButtonItem *)btn{

    DebarkViewController *debarkVC = [[DebarkViewController alloc] init];
    debarkVC.fourViewController = self;
    [self.navigationController pushViewController:debarkVC animated:YES];
    
}

-(void)rightAction1: (UIBarButtonItem *)button{

    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定注销用户？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"user"];
        
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        
        
    }];
    [alertC addAction:action];
    [alertC addAction:action1];
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 2) {
        NSUInteger size = [[SDImageCache sharedImageCache] getSize];
        float size1 = size / 1024.0 / 1024.0;
        

        NSString *str = [NSString stringWithFormat:@"缓存大小为：%.2fM，确定清理缓存？", size1];
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            [[SDImageCache sharedImageCache] clearDisk];
            
            

            
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            
            
        }];
        [alertC addAction:action];
        [alertC addAction:action1];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
    }
    else if(indexPath.row == 1){
       CollectViewController *collectVC = [[CollectViewController alloc] init];
        [self.navigationController pushViewController:collectVC animated:YES];
        [collectVC release];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
